package com.wpp.mq.rabbitmqsqmple.direct;

import com.rabbitmq.client.*;
import com.wpp.mq.rabbitmqsqmple.factory.MyConnectionFactoryBuilder;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @description: 消费者
 * @author: WangPengPeng
 * @create: 2021-11-28 21:52
 **/
public class ReceiveInfoLogsDirect {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = MyConnectionFactoryBuilder.buildConnection();
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        String queueName = channel.queueDeclare().getQueue();
        // 将logs交换器和queueName队列通过bindingKey：error绑定
        channel.queueBind(queueName, "direct_logs", "info");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume(queueName, deliverCallback, consumerTag -> {
        });
    }
}

